package fr.inserm.u915.tabixserver;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.List;

import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import fr.inserm.u915.tabixserver.segparser.ParseException;
import fr.inserm.u915.tabixserver.segparser.SegmentParser;

@SuppressWarnings("serial")
public class SVGServlet extends HttpServlet
	{
	private int max_count_objects=10000;
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		String s=config.getInitParameter("max-objects");
		if(s!=null)
			{
			this.max_count_objects=Integer.parseInt(s);
			}
		}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
			{
			final Model model=(Model)this.getServletContext().getAttribute(TabixCtxListener.TABIX_MODEL_ATTRIBUTE);
			if(model==null) throw new ServletException("model?");
			Build build=(Build)req.getAttribute("build");
			if(build==null)  throw new ServletException("build attribute missing.");
			
			
			List<Segment> segments=(List<Segment>)req.getAttribute("segments");
			if(segments==null) throw new ServletException("segs?");
			if(segments.size()!=1) throw new ServletException("only one segment allowed");
			
			Set<String> instancesIds=new HashSet<String>();
			String ts[]=req.getParameterValues("t");
			if(ts!=null)
				{
				for(String s:ts)
					{
					instancesIds.add(s);
					}
				}
			if(instancesIds.isEmpty())
				{
				throw new ServletException("Not table selected");
				}
			
			
			OutputStream out=resp.getOutputStream();
			SVGBuilder svgBuilder=new SVGBuilder();
			svgBuilder.setBuild(build);
			svgBuilder.setSegment(segments.get(0));
			svgBuilder.setInstanceIds(instancesIds);
			
			
			Document dom=svgBuilder.makeDocument();
	        try {
				TransformerFactory factory=TransformerFactory.newInstance();
	        	resp.setContentType("image/svg+xml");
				Transformer transformer=factory.newTransformer();
				transformer.setOutputProperty(OutputKeys.ENCODING, resp.getCharacterEncoding());
				transformer.transform(new DOMSource(dom), new StreamResult(out));
			} catch (TransformerConfigurationException e)
				{
				throw new ServletException(e);
				}
	        catch (TransformerException e) {
				throw new ServletException(e);
				}
	        finally
	        	{
	        	out.flush();
	        	out.close();
	        	}
			}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
		}
	}
